﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Net;
using System.Net.Browser;


namespace WpWordpressJson
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }

        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData()
        {
            WebRequest.RegisterPrefix("http://MYBLOGURL", WebRequestCreator.ClientHttp);
            Uri serviceUri = new Uri("http://MYBLOGURL/?json=get_recent_posts");
            WebClient downloader = new WebClient();
            downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
            downloader.OpenReadAsync(serviceUri);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                try
                {
                    Stream responseStream = e.Result;
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Posts));
                    Posts response = (Posts)ser.ReadObject(responseStream);
                    if (response.posts != null && response.posts.Count > 0)
                    {
                        foreach (Post post in response.posts)
                        {
                            this.Items.Add(new ItemViewModel() { LineOne = post.title, LineTwo = post.excerpt });
                        }
                    }
                }
                catch (Exception x)
                {
                    return;
                }
                this.IsDataLoaded = true;
            }
        }
    }

    [DataContract]
    public class Post
    {
        [DataMember]
        public int id;
        [DataMember]
        public string type;
        [DataMember]
        public string slug;
        [DataMember]
        public string title;
        [DataMember]
        public string content;
        [DataMember]
        public string excerpt;
    }

    [DataContract]
    public class Posts
    {
        [DataMember]
        public int count;
        [DataMember]
        public int count_total;
        [DataMember]
        public List<Post> posts;
    }
}